package com.lmonkey.servlet.cart;

import com.lmonkey.dao.Basedao;
import com.lmonkey.servic.ADDRESSDao;
import com.lmonkey.servic.CARTDao;
import com.lmonkey.servic.CATAGORYDao;
import com.lmonkey.servic.ORDERDao;
import com.lmonkey.shop.ADDRESS;
import com.lmonkey.shop.CART;
import com.lmonkey.shop.CATEGORY;
import com.lmonkey.shop.USER;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@WebServlet("/shangc/orderselect")
public class OrderSelect extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        //获取session对象
        HttpSession session = request.getSession();
        String isLogin = (String)session.getAttribute("isLogin");
        USER user = (USER)session.getAttribute("name");

        //菜单
        ArrayList<CATEGORY> flist = CATAGORYDao.selectcat("father");
        request.setAttribute("flist",flist);
        ArrayList<CATEGORY> clist = CATAGORYDao.selectcat("child");
        request.setAttribute("clist",clist);

        //获取数据
        String eids = request.getParameter("eids");
        request.setAttribute("ids",eids);


        //判断用户是否登录
        if (user != null && isLogin.equals("1")){
            String uid = user.getuser_id();
            int count = CARTDao.selectByid(uid);
            request.setAttribute("num",count);

            float totalprice = 0;
            String ids[] = eids.split(",");
            ArrayList<CART> list = new ArrayList<CART>();

            for (int i = 0;i<ids.length;i++){
                CART es = CARTDao.getcartshop(ids[i]);
                    float dprice = es.getCart_quantity() * es.getCart_p_price();
                    totalprice += dprice;
                    list.add(es);
                    int count1 = ORDERDao.selectByid4(es.getCart_id());
                    if (count1<=0){
                        //将商品加入订单中
                        ORDERDao.add(es.getCart_p_price(), es.getCart_quantity(), es.getCart_p_name(), es.getCart_p_filename(),es.getCart_id(),user.getuser_id(),user.getuser_name());
                    }
                    //删除购物车里的商品
                    CARTDao.update(Integer.parseInt(ids[i]));
                }
            //编辑地址的id
            String did = request.getParameter("did");
            //查找信息
            if (did != null){
                ADDRESS aress = ADDRESSDao.selectByid3(did);
                request.setAttribute("are",aress);
            }


            //查找地址
            ArrayList<ADDRESS> ad = ADDRESSDao.selectByid1(user.getuser_id());
            request.setAttribute("ad",ad);
            //查找默认地址
            ADDRESS ds = ADDRESSDao.selectByid2(user.getuser_id());
            request.setAttribute("ds",ds);

            request.setAttribute("shoplist",list);
            request.setAttribute("totalprice",totalprice);
            request.getRequestDispatcher("order.jsp").forward(request,response);
        }else {
            PrintWriter out = response.getWriter();
            out.println("<script>alert('请先登录');location.href='login.jsp';</script>");
            out.close();
            return;
        }
    }
}
